package com.ruoyi.orders.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.management.domain.CheOrder;
import com.ruoyi.user.api.RemoteOrdersService;
import com.ruoyi.user.api.RemoteUsersService;

import com.ruoyi.user.api.domain.CheService;
import com.ruoyi.user.api.domain.CheUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.orders.domain.CheEvaluate;
import com.ruoyi.orders.service.ICheEvaluateService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 订单评价Controller
 * 
 * @author ruoyi
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/orders")
public class CheEvaluateController extends BaseController
{
    @Autowired
    private ICheEvaluateService cheEvaluateService;


    @Autowired
    private RemoteUsersService remoteUsersService;

    @Autowired
    private RemoteOrdersService remoteOrdersService;

    /**
     * 查询订单评价列表
     */
    @RequiresPermissions("orders:orders:list")
    @GetMapping("/list")
    public TableDataInfo list(CheEvaluate cheEvaluate)
    {
        startPage();
        List<CheEvaluate> list = cheEvaluateService.selectCheEvaluateList(cheEvaluate);
        for (CheEvaluate cheEvaluate1 : list){
             Long userId = cheEvaluate1.getUserId();
             AjaxResult info = remoteUsersService.getInfo(userId, SecurityConstants.INNER);
            CheUser data = JSON.parseObject(JSON.toJSONString(info.get("data")), CheUser.class);
            cheEvaluate1.setCheUser(data);
            Long orderId = cheEvaluate1.getOrderId();
            AjaxResult info1 = remoteOrdersService.getInfo(orderId, SecurityConstants.INNER);
            CheOrder data1 = JSON.parseObject(JSON.toJSONString(info1.get("data")), CheOrder.class);
            cheEvaluate1.setCheOrder(data1);
        }

        return getDataTable(list);
    }

    /**
     * 导出订单评价列表
     */
    @RequiresPermissions("orders:orders:export")
    @Log(title = "订单评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CheEvaluate cheEvaluate)
    {
        List<CheEvaluate> list = cheEvaluateService.selectCheEvaluateList(cheEvaluate);
        ExcelUtil<CheEvaluate> util = new ExcelUtil<CheEvaluate>(CheEvaluate.class);
        util.exportExcel(response, list, "订单评价数据");
    }

    /**
     * 获取订单评价详细信息
     */
    @RequiresPermissions("orders:orders:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        CheEvaluate cheEvaluate1 = cheEvaluateService.selectCheEvaluateById(id);
        Long userId = cheEvaluate1.getUserId();
        AjaxResult info = remoteUsersService.getInfo(userId, SecurityConstants.INNER);
        CheUser data = JSON.parseObject(JSON.toJSONString(info.get("data")), CheUser.class);
        cheEvaluate1.setCheUser(data);
        Long orderId = cheEvaluate1.getOrderId();
        AjaxResult info1 = remoteOrdersService.getInfo(orderId, SecurityConstants.INNER);
        CheOrder data1 = JSON.parseObject(JSON.toJSONString(info1.get("data")), CheOrder.class);
        cheEvaluate1.setCheOrder(data1);
        return success(cheEvaluate1);
    }

    /**
     * 新增订单评价
     */
    @RequiresPermissions("orders:orders:add")
    @Log(title = "订单评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CheEvaluate cheEvaluate)
    {
        return toAjax(cheEvaluateService.insertCheEvaluate(cheEvaluate));
    }

    /**
     * 修改订单评价
     */
    @RequiresPermissions("orders:orders:edit")
    @Log(title = "订单评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CheEvaluate cheEvaluate)
    {
        return toAjax(cheEvaluateService.updateCheEvaluate(cheEvaluate));
    }

    /**
     * 删除订单评价
     */
    @RequiresPermissions("orders:orders:remove")
    @Log(title = "订单评价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cheEvaluateService.deleteCheEvaluateByIds(ids));
    }
}
